package com.example.libraryms.utils;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.ToString;

import java.io.Serializable;
import java.util.List;

/**
 * @author Cauli
 * @date 2023/7/31 11:55
 * @description Ajax请求返回JSON格式数据的封装
 */
@Data
@ToString
public class ResultResponse<T> implements Serializable {
    @ApiModelProperty(value = "请求是否成功", example = "true")
    private Boolean success;

    @ApiModelProperty(value = "业务状态码", example = "200")
    private Integer code;

    @ApiModelProperty(value = "响应消息", example = "ok")
    private String message;

    @ApiModelProperty(value = "响应数据")
    private T data;

    @ApiModelProperty(value = "数据集合")
    private List<T> dataList;

    @ApiModelProperty(value = "数据总条数，分页情况有效")
    private Long total;

    public ResultResponse() {
    }

    public static ResultResponse success() {
        ResultResponse r = new ResultResponse();
        r.setSuccess(ResultCodeEnum.SUCCESS.getSuccess());
        r.setCode(ResultCodeEnum.SUCCESS.getCode());
        r.setMessage(ResultCodeEnum.SUCCESS.getMessage());
        return r;
    }

    public static ResultResponse fail() {
        ResultResponse r = new ResultResponse();
        r.setSuccess(ResultCodeEnum.UNKNOWN_ERROR.getSuccess());
        r.setCode(ResultCodeEnum.UNKNOWN_ERROR.getCode());
        r.setMessage(ResultCodeEnum.UNKNOWN_ERROR.getMessage());
        return r;
    }

    public static ResultResponse setResult(ResultCodeEnum resultCodeEnum) {
        ResultResponse r = new ResultResponse();
        r.setSuccess(resultCodeEnum.getSuccess());
        r.setCode(resultCodeEnum.getCode());
        r.setMessage(resultCodeEnum.getMessage());
        return r;
    }

    public ResultResponse success(Boolean success) {
        this.setSuccess(success);
        return this;
    }

    public ResultResponse code(Integer code) {
        this.setCode(code);
        return this;
    }

    public ResultResponse data(T param) {
        this.setData(param);
        return this;
    }

    public ResultResponse message(String message) {
        this.setMessage(message);
        return this;
    }

    public ResultResponse dataList(List<T> dataList, Long total) {
        this.setDataList(dataList);
        this.setTotal(total);
        return this;
    }
}
